# __author__ : htzs
# __time__   : 19-6-20 下午6:10

from io import BytesIO
import datetime

import xlwt

from django.http import HttpResponse
from django.db.models import Q

from apps.warnlist.models import WarnModel

from apps.warnlist.func_select import select_data


def make_image(obj):
    """
    获取预警图片地址列表
    :param obj:
    :return:
    """
    image_list = []
    if obj:
        for i in obj:
            image_list.append(i.image_url)
        return image_list


def make_images(obj):
    """
    获取城管图片地址列表
    :param obj:
    :return:
    """
    images_list = []
    if obj:
        for i in obj:
            images_list.append(i.images_url)
        return images_list


def return_excel(request):
    type_id = int(request.GET.get('type_id', 1))
    response = HttpResponse(content_type='application/ms-excel')
    timestr = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    response['Content-Disposition'] = 'attachment; filename="{0}.xls"'.format(timestr)
    file = xlwt.Workbook(encoding='utf-8')  # 文件对象
    sheet = file.add_sheet('预警数据', cell_overwrite_ok=True)  # 生成表格底部选项卡名称对象
    row_num = 0
    font_style = xlwt.XFStyle()
    font_style.font.bold = True
    columns = ['预警编号', '任务类型', '预警类型', '所属监控点', '事件图片', '证据图片', '证据视频', '发生时间']

    # 写入数据
    warn_list = WarnModel.objects.filter(is_make=1)

    if type_id == 1:
        # 已发送
        col_list = ['当前状态', '平台处理员']
        columns.extend(col_list)
        warn_list = WarnModel.objects.filter(is_make=2)

    elif type_id == 2:
        # 已完成
        col_list = ['执法图片', '执法意见', '平台处理员', '城管处理员']
        columns.extend(col_list)
        warn_list = WarnModel.objects.filter(is_make=3)

    elif type_id == 3:
        # 已丢弃
        col_list = ['当前状态', '平台处理员', '城管处理员']
        columns.extend(col_list)
        warn_list = WarnModel.objects.filter(Q(is_make=4) | Q(is_make=5))

    elif type_id == 4:
        # 预警分析
        warn_list = warn_list

    obj_lists = select_data(request, warn_list)

    # 写入表头
    for col_num in range(len(columns)):
        sheet.write(row_num, col_num, columns[col_num], font_style)

    row_num = 1
    # 写入数据
    for row in obj_lists:
        event_id = row.event_id
        warn_manage = row.warn_manage.get_type_name_display()
        warn_type = row.get_warn_type_display()
        map = row.map.name
        warn_url = row.warn_url
        warn_image = make_image(row.rec_image())
        video_url = row.video_url
        add_time = row.add_time.strftime('%Y-%m-%d %H:%M:%S')

        sheet.write(row_num, 0, event_id)
        sheet.write(row_num, 1, warn_manage)
        sheet.write(row_num, 2, warn_type)
        sheet.write(row_num, 3, map)
        sheet.write(row_num, 4, warn_url)
        sheet.write(row_num, 5, warn_image)
        sheet.write(row_num, 6, video_url)
        sheet.write(row_num, 7, add_time)

        if type_id == 1:
            result = row.result
            user = request.user.username
            sheet.write(row_num, 8, result)
            sheet.write(row_num, 9, user)

        elif type_id == 2:
            result = row.result
            user = request.user.username
            manage_user = row.manage_user
            images_url = make_images(row.manage_image())
            sheet.write(row_num, 8, images_url)
            sheet.write(row_num, 9, result)
            sheet.write(row_num, 10, user)
            sheet.write(row_num, 11, manage_user)

        elif type_id == 3:
            result = row.result
            user = request.user.username
            manage_user = row.manage_user
            sheet.write(row_num, 8, result)
            sheet.write(row_num, 9, user)
            sheet.write(row_num, 10, manage_user)

        row_num += 1

    file.save(response)
    return response
